package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServiceInventoryCheck;
import com.eastfair.venueservice.service.ServiceInventoryCheckService;
import com.eastfair.venueservice.vo.ServiceInventoryCheckVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 盘点记录
 * </p>
 *
 * @author linan
 * @date 2022-09-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceInventoryCheck")
@Api(value = "ServiceInventoryCheck", tags = "盘点记录")
@PreAuth(replace = "venueservice:serviceInventoryCheck:")
public class ServiceInventoryCheckController extends SuperController<ServiceInventoryCheckService, Long, ServiceInventoryCheck, ServiceInventoryCheckPageQuery, ServiceInventoryCheckSaveDTO, ServiceInventoryCheckUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ServiceInventoryCheck> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceInventoryCheck> serviceInventoryCheckList = list.stream().map((map) -> {
            ServiceInventoryCheck serviceInventoryCheck = ServiceInventoryCheck.builder().build();
            //TODO 请在这里完成转换
            return serviceInventoryCheck;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceInventoryCheckList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceInventoryCheckPageQuery> params) {
        ServiceInventoryCheckPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 新增盘点记录
     * @param serviceInventoryCheckDTO
     * @return
     */
    @ApiOperation(value = "新增盘点记录")
    @SysLog("添加盘点记录")
    @PostMapping(value = "/saveInventoryCheck")
    public R<Boolean> saveInventoryCheck(@RequestBody ServiceInventoryCheckDTO serviceInventoryCheckDTO){
        try {
            return R.success(baseService.saveInventoryCheck(serviceInventoryCheckDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 修改盘点记录
     * @param serviceInventoryCheckDTO
     * @return
     */
    @ApiOperation(value = "修改盘点记录")
    @SysLog("修改盘点记录")
    @PostMapping(value = "/updateInventoryCheck")
    public R<Boolean> updateInventoryCheck(@RequestBody ServiceInventoryCheckDTO serviceInventoryCheckDTO){
        try {
            return R.success(baseService.updateInventoryCheck(serviceInventoryCheckDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 删除盘点记录
     * @param serviceInventoryCheckDTO
     * @return
     */
    @ApiOperation(value = "删除盘点记录")
    @SysLog("删除盘点记录")
    @PostMapping(value = "/delInventoryCheck")
    public R<Boolean> delInventoryCheck(@RequestBody ServiceInventoryCheckDTO serviceInventoryCheckDTO){
        try {
            return R.success(baseService.delInventoryCheck(serviceInventoryCheckDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询盘点记录
     * @param serviceInventoryCheckQuery
     * @return
     */
    @ApiOperation(value = "查询盘点记录")
    @PostMapping(value = "/queryCheckList")
    public R<Page<ServiceInventoryCheckVO>> queryCheckList(@RequestBody PageParams<ServiceInventoryCheckQuery> serviceInventoryCheckQuery){
        log.info("queryCheckList - 查询盘点记录, serviceInventoryCheckQuery={}", serviceInventoryCheckQuery);
        try {
            return R.success(baseService.queryCheckList(serviceInventoryCheckQuery));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询盘点记录明细
     * @param serviceInventoryCheckQuery
     * @return
     */
    @ApiOperation(value = "查询盘点记录明细")
    @PostMapping(value = "/queryCheckDetailList")
    public R<ServiceInventoryCheckVO> queryCheckDetailList(@RequestBody ServiceInventoryCheckQuery serviceInventoryCheckQuery){
        log.info("queryCheckDetailList - 查询盘点记录明细, serviceInventoryCheckQuery={}", serviceInventoryCheckQuery);
        try {
            return R.success(baseService.queryCheckDetailList(serviceInventoryCheckQuery));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询新的盘点单号
     * @return
     */
    @ApiOperation(value = "查询新的盘点单号")
    @GetMapping(value = "/queryNewCheckNo")
    public R<String> queryNewCheckNo(){
        log.info("queryNewCheckNo - 查询新的盘点单号");
        try {
            return R.success(baseService.queryNewCheckNo());
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

}
